package org.zhouhe.tag.report;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zhouhe.modules.imageSlider.service.ImageSliderService;
import org.zhouhe.vo.ImageSlider;

public class ReportSlider extends TagSupport {

	private static final long serialVersionUID = 3886850494335003326L;
	private ServletContext servletContext;
	private WebApplicationContext wac;

	private Integer typeId;
	private Integer industryId;
	private Integer resultSize = 10;
	private Integer height = 100;
	private Integer width = 100;
	private Integer titleHeight = 18;
	private String className;
	private ImageSliderService service;

	public int doStartTag() {
		try {
			StringBuilder html = new StringBuilder();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			servletContext = request.getSession().getServletContext();
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			service = (ImageSliderService) wac.getBean("imageSliderService");
//			List<ImageSlider> imageSliderList = (List<ImageSlider>) servletContext.getAttribute(Constant.APPLICATION_KEY_SLIDER_LIST);
			List<ImageSlider> imageSliderList = service.getImageSliderList(null);
			String imageUrls = "";
			String imageLinks = "";
			String imageTexts = "";
			for(ImageSlider slider : imageSliderList){
				if(slider.getArticle() != null){
					imageUrls  += "|" + slider.getArticle().getImgName();
					imageLinks += "|article.action?article.articleId=" + slider.getArticle().getArticleId();
					imageTexts += "|" + slider.getArticle().getTitle();
				}
				if(slider.getReport() != null){
					imageUrls  += "|" + slider.getReport().getImgName();
					imageLinks += "|report.action?report.reportId=" + slider.getReport().getReportId();
					imageTexts += "|" + slider.getReport().getTitle();
				}
			}
			if(imageUrls.length() > 0){
				imageUrls  = imageUrls.substring(1);
				imageLinks = imageLinks.substring(1);
				imageTexts = imageTexts.substring(1);
			}
//			html.append("<div style='height:"+ height +"; width:"+ width +"; overflow:hidden;'>\n");
			html.append("<script type=\"text/javascript\">\n");
			html.append("var focus_width  = "+ width +";\n");
			html.append("var focus_height = "+ (height - titleHeight) +";\n");
			html.append("var text_height  = "+ titleHeight +";\n");
			html.append("var swf_height = "+ height +";\n");
			html.append("var pics  ='"+ imageUrls +"';\n");
			html.append("var links ='"+ imageLinks +"';\n");
			html.append("var texts ='"+ imageTexts +"'; \n");
			html.append("var jshtml = ''; ");
			html.append("    jshtml += '<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"'+ focus_width +'\" height=\"'+ swf_height +'\">';\n");
			html.append("    jshtml += '<param name=\"allowScriptAccess\" value=\"sameDomain\"/>';  \n");
			html.append("    jshtml += '<param name=\"movie\" value=\"images/slidenews.swf\"/>';  \n ");
			html.append("    jshtml += '<param name=\"quality\" value=\"high\"/>';  \n");
			html.append("    jshtml += '<param name=\"bgcolor\" value=\"#F0F0F0\"/>';  \n");
			html.append("    jshtml += '<param name=\"menu\" value=\"false\"/>';  \n");
			html.append("    jshtml += '<param name=\"wmode\" value=\"opaque\"/>';  \n");
			html.append("    jshtml += '<param name=\"FlashVars\" value=\"pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\"/>';  \n");
			html.append("    jshtml += '<embed src=\"images/slidenews.swf\" wmode=\"opaque\" FlashVars=\"pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\" menu=\"false\" bgcolor=\"#F0F0F0\" quality=\"high\" width=\"'+ focus_width +'\" height=\"'+ swf_height +'\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"/>';  \n");
			html.append("    jshtml += '</object>';  \n");
			html.append("document.write(jshtml);");
			html.append("</script>\n");
//			html.append("</div>\n");
			
//			html.append("<script type=\"text/javascript\"> ");
//			html.append(" ");
//			html.append("var focus_width = 230;");
//			html.append("var focus_height = 210;");
//			html.append("var text_height = 0;");
//			html.append("var swf_height = focus_height+text_height;");
//			html.append("var pics =\"upload/images/report_img/201103/2011.03.11.02.55.49.jpg|upload/images/report_img/201103/2011.03.11.02.56.00.jpg|upload/images/report_img/201103/2011.03.11.02.56.11.jpg|upload/images/report_img/201103/2011.03.11.02.56.21.jpg\";");
//			html.append("var links =\"report.action?report.reportId=352|report.action?report.reportId=351|report.action?report.reportId=337|report.action?report.reportId=323\";");
//			html.append("document.write('<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"'+ focus_width +'\" height=\"'+ swf_height +'\">');");
//			html.append("document.write('<param name=\"allowScriptAccess\" value=\"sameDomain\"><param name=\"movie\" value=\"/images/slidenews.swf\"><param name=\"quality\" value=\"high\"><param name=\"bgcolor\" value=\"#F0F0F0\">');");
//			html.append("document.write('<param name=\"menu\" value=\"false\"><param name=\"wmode\" value=\"opaque\">');");
//			html.append("document.write('<param name=\"FlashVars\" value=\"pics='+pics+'&links='+links+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\">');");
//			html.append("document.write('<embed src=\"images/slidenews.swf\" wmode=\"opaque\" FlashVars=\"pics='+pics+'&links='+links+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\" menu=\"false\" bgcolor=\"#F0F0F0\" quality=\"high\" width=\"'+ focus_width +'\" height=\"'+ focus_height +'\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"/>');");
//			html.append("document.write('</object>');");
//			html.append("</script>");
			JspWriter out = pageContext.getOut();
			out.print(html.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}

	public Integer getTypeId() {
		return typeId;
	}

	public void setResultSize(Integer resultSize) {
		this.resultSize = resultSize;
	}

	public Integer getResultSize() {
		return resultSize;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getClassName() {
		return className;
	}

	public void setIndustryId(Integer industryId) {
		this.industryId = industryId;
	}

	public Integer getIndustryId() {
		return industryId;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getHeight() {
		return height;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getWidth() {
		return width;
	}

	public void setTitleHeight(Integer titleHeight) {
		this.titleHeight = titleHeight;
	}

	public Integer getTitleHeight() {
		return titleHeight;
	}

}
